using System;
using System.Collections.Generic;
using System.Text;
using NBusiness.CodeDom;
using NBusiness.Data.Rules;

namespace NBusiness.Data.Rules.Validation
{
    [Serializable]
    //[ExpectedArgumentType(typeof(MaxValueEventArgs))]
    public sealed class MaxValueRule : ValidationRule
    {
        public MaxValueRule(Type type, string field, MaxValueEventArgs arg)
            : base(type, field, ValidateMaxValueRule, arg, "Resources.MaxValueRuleMessage")
        { }

        public static bool ValidateMaxValueRule(object value, ValidationEventArgs e)
        {
            MaxValueEventArgs arg = e as MaxValueEventArgs;
            bool retVal = true;
            if (value != null)
            {

                IComparable comparable = (IComparable)Convert.ChangeType(arg.Value, value.GetType(), System.Globalization.CultureInfo.InvariantCulture);
                retVal = comparable.CompareTo(value) > -1;
            }
            return retVal;
        }
    }
}
